﻿package domino 
{
	import Box2D.Common.Math.b2Vec2;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.geom.Point;
	import flash.display.Sprite;
	
	import Box2D.Dynamics.*;
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	
	/**
	 * ...
	 * @author ...
	 */
	public class actorDomino extends actor
	{
		private static const BALL_W:int = 16;
		private static const BALL_H:int = 64;
		    var final_body:b2Body;
			var the_body:b2BodyDef;
			var the_box:b2PolygonDef;
			public var stat:Number;
			public var dominSprite:Sprite ;
			public var scaleX, scaleY;
			public var density, angV;
			public var isSensor:int;
			//public var restitution;
		public function actorDomino(_world:b2World,parent:DisplayObjectContainer,dominPosition:Point,_rot:Number,_density,_friction,_scaleX,_scaleY,_restitution:Number,_isSensor:int) 
		{
			//create costume
		    dominSprite = new domRect();
            dominSprite.scaleX = _scaleX;
			dominSprite.scaleY = _scaleY;
			scaleX = _scaleX;
			scaleY = _scaleY;
			dominSprite.x = dominPosition.x;
			dominSprite.y = dominPosition.y;
			//dominSprite.rotation = _rot;
			parent.addChild(dominSprite);
			 
			//create body
			isSensor = _isSensor;
			the_body = new b2BodyDef();
			the_body.position.Set(dominSprite.x / 30, dominSprite.y / 30);
			the_body.angle = _rot * Math.PI / 180;

			the_box = new b2PolygonDef();
			trace(dominSprite.width+ " "+ dominSprite.height);
			the_box.SetAsBox(dominSprite.width / 60 , dominSprite.height / 60);
	//		trace(dominSprite.width + " " + dominSprite.height);
	        the_box.restitution = _restitution;
			//restitution = _restitution;
			the_box.friction = _friction;	
			density = _density;
			angV = 0;
			the_box.density = _density;
			the_box.isSensor = isSensor;
			final_body = _world.CreateBody(the_body);
			final_body.AllowSleeping(true);
			final_body.CreateShape(the_box);
		    final_body.SetXForm(new b2Vec2(final_body.GetPosition().x,final_body.GetPosition().y), _rot * Math.PI/180);
			final_body.SetMassFromShapes();
			
			
			super(final_body, dominSprite);
			setMovie(_density);
		}
		
		public function changeScale(x, y) {
			
			_body.DestroyShape(_body.GetShapeList());
			
			_costume.scaleX *= x;
			_costume.scaleY *= y;
			}
		
			
		function setMovie(s:Number) {
			if(s==0)
			 MovieClip(_costume).gotoAndStop(2);
			else
			 MovieClip(_costume).gotoAndStop(1);
			 
			}
			///////////////////////////////////
	public function getData():Array {
		var data:Array = new Array();
		data.push(_costume.x);
		data.push(_costume.y);
		data.push(dominSprite.scaleX);
		data.push(dominSprite.scaleY);
		data.push(_body.GetShapeList().GetFriction());
		data.push(_body.GetShapeList().m_density);
		data.push(_costume.rotation);
		data.push(_body.GetShapeList().GetRestitution());
		data.push(0);
		
		return data;
	 }
	 
	
	}

}